<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>评论列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="./css/reset.css" />
    <link rel="stylesheet" href="./css/iconfont.css" />
    <link rel="stylesheet" href="./css/main.css" />
    <link rel="stylesheet" href="./libs/layui/css/layui.css" />
    <script src="./libs/jquery/jquery.min.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">评论列表</div>
      <div class="container-fluid common_con">
        <table class="table table-striped table-bordered table-hover mp20">
          <thead>
            <tr>
              <th>作者</th>
              <th>评论</th>
              <th>评论在</th>
              <th>提交于</th>
              <th>状态</th>
              <th class="text-center" width="100">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>小周</td>
              <td>这是一条测试评论，欢迎光临</td>
              <td>《世界，你好》</td>
              <td>2017-07-04 12:00:00</td>
              <td>已批准</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-warning btn-xs"
                  >拒绝</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr class="danger">
              <td>中周</td>
              <td>How are you?</td>
              <td>《世界，你好》</td>
              <td>2017-07-09 18:22:00</td>
              <td>待审核</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-info btn-xs"
                  >批准</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>

            <tr>
              <td>武秀英</td>
              <td>
                外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。
              </td>
              <td>《世界，你好》</td>
              <td>1970-03-15 11:31:50</td>
              <td>已拒绝</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
          </tbody>
        </table>
        <div class="row text-center">
          <!-- <ul class="pagination pagination-sm">
            <li class="page-item first disabled">
              <a href="#" class="page-link">首页</a>
            </li>
            <li class="page-item prev disabled">
              <a href="#" class="page-link">上一页</a>
            </li>
            <li class="page-item active">
              <a href="#" class="page-link">1</a>
            </li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">4</a></li>
            <li class="page-item"><a href="#" class="page-link">5</a></li>
            <li class="page-item"><a href="#" class="page-link">6</a></li>
            <li class="page-item"><a href="#" class="page-link">7</a></li>
            <li class="page-item next">
              <a href="#" class="page-link">下一页</a>
            </li>
            <li class="page-item last">
              <a href="#" class="page-link">尾页</a>
            </li>
          </ul> -->
          <ul id="pagination" class="pagination-sm"></ul>
        </div>
      </div>
    </div>
    <script src="./js/jqconfig.js"></script>
    <!-- 定义一个模板 -->
    <script src="./libs/art-template/template-web.js"></script>
    <script type="text/html" id="list">
      {{each data}} {{if $value.state=='已拒绝'}}
      <tr class="danger">
        {{else}}

      <tr>
        {{/if}}

        <td>{{$value.author}}</td>
        <td>{{$value.content}}</td>
        <td>{{$value.title}}</td>
        <td>{{$value.date}}</td>
        <td>{{$value.state}}</td>
        <td class="text-center">
          {{if $value.state=='已通过'}}
          <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>

          {{else if $value.state=='待审核'}}
          <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="ratify({{$value.id}})" >批准</a>

          <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>
          {{else}}
          <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="ratify({{$value.id}})">批准</a>

          {{/if}}
          <a
            href="javascript:void(0);;"
            class="btn btn-danger btn-xs "
            onclick="dellist({{$value.id}})"
            >删除</a
          >
        </td>
      </tr>

      {{/each}}
    </script>
    <script src="./libs/layui/layui.js"></script>
    <script>
      var laypage, layer;
      layui.use("laypage", function () {
        laypage = layui.laypage;

        comment();
        
      });
      layui.use("layer", function () {
        layer = layui.layer;
      });
      // 获取评论所有的数据
      let parms = { page: 1, perpage: 6 };
      function comment() {
        $.ajax({
          type: "get",
          url: "/admin/comment/search",
          data: parms,
          success: (res) => {
            console.log(res);
            // 将数据渲染到页面
            $("tbody").html(template("list", res.data));
            //执行一个laypage实例
            laypage.render({
              elem: "pagination", //注意，这里的 test1 是 ID，不用加 # 号
              count: res.data.totalCount, //数据总数，从服务端得到
              curr: parms.page, //起始页
              jump: function (obj, first) {
                if (!first) {
                  parms.page = obj.curr;
                  comment();
                }
              },
            });
          },
        });
      }

      // // 点击有类
      // $("tbody").on("click", "tr", function () {
      //   $(this).addClass("danger").siblings().removeClass("danger");
      // });

      // 点击删除
      function dellist(id) {
        console.log(id);
        layer.confirm(
          "是否确认删除?",
          { icon: 3, title: "删除提示" },
          function (index) {
            //do something

            layer.close(index);

            $.ajax({
              type: "post",
              url: "/admin/comment/delete",
              data: { id },
              success: (res) => {
                console.log(res);
                comment();
              },
            });
          }
        );
      }
  
      //点击批准
      function ratify(id){
        $.ajax({
          type: 'post',
          url: '/admin/comment/pass',
          data: {id},
          success: (res) => {
            console.log(res)
            console.log(1);
            comment()
          },
        })
      }
  //点击拒绝
  function reject(id){
    $.ajax({
      type: 'post',
      url: '/admin/comment/reject',
      data: {id},
      success: (res) => {
        console.log(res)
        comment()
      },
    })
  }
  </script>
  </body>
</html>
